package com.saris.primego.web.controller.admin;

import com.saris.primego.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController("adminShopController")
@RequestMapping("/admin/shop")
@Tag(name = "店铺管理")
@Slf4j
public class ShopController {
    final RedisTemplate<String, Object> redisTemplate;

    public ShopController(@Qualifier("objectRedisTemplate") RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @PutMapping("/{status}")
    @Operation(summary = "设置状态", method = "PUT")
    public Result<String> setStatus(@PathVariable int status) {
        log.info("设置店铺状态为：{}", status == 1 ? "营业中" : "打烊中");

        redisTemplate.opsForValue().set("SHOP_STATUS", status);

        return Result.success(status == 1 ? "营业中" : "打烊中");
    }


    @GetMapping("/status")
    @Operation(summary = "查询状态", method = "GET")
    public Result<Integer> getStatus() {
        Integer shopStatus = (Integer) redisTemplate.opsForValue().get("SHOP_STATUS");
        return Result.success(shopStatus != null ? shopStatus : 0);
    }
}
